package com.zzyl.serve.controller;

import java.util.List;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.serve.service.IDeviceService;

/**
 * 设备Controller
 *
 * @author baefengk2
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 从物联网平台同步产品列表
     * <p>
     * 接口地址:/nursing/device/syncProductList
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping("/syncProductList")
    @ApiOperation(value = "获取设备列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return AjaxResult.success();
    }


    /**
     * 查询所有产品列表
     * 接口地址:/nursing/device/allProduct
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return AjaxResult.success(list);
    }

    /**
     * 注册设备
     * 接口地址:/nursing/device/register
     * 请求方式:POST
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return AjaxResult.success();
    }

    /**
     * 查询设备列表
     * 接口地址:/nursing/device/list
     * 请求方式:GET
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 查询设备详情
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:GET
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "查询设备详情")
    public R<DeviceDetailVo> getDeviceDetail(@PathVariable String iotId) {
        DeviceDetailVo detailVo = deviceService.getDeviceDetail(iotId);
        return R.ok(detailVo);
    }

    /**
     * 查看设备最新上报的数据
     * 接口地址: /nursing/device/queryServiceProperties/{iotId}
     * 请求方式:GET
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查看设备上报的数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        return deviceService.queryServiceProperties(iotId);
    }

    /**
     * 修改设备
     * 接口地址:/nursing/device
     * 请求方式:PUT
     */
    @PutMapping
    @ApiOperation(value = "修改设备")
    public AjaxResult edit(@RequestBody DeviceDto dto) {
        deviceService.edit(dto);
        return AjaxResult.success();
    }

    /**
     * 删除设备
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:DELETE
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备")
    public AjaxResult remove(@PathVariable String iotId) {
        deviceService.remove(iotId);
        return AjaxResult.success();
    }

    /**
     * 查询产品详情(报警规则模块使用)
     * 接口地址:/nursing/device/queryProduct/{productKey}
     * 请求方式:GET
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey) {
        AjaxResult result = deviceService.queryProduct(productKey);
        return result;
    }



}
